
import 'package:flutter/services.dart';
import 'package:flutterlib/common/utils/sp_utils.dart';
import 'package:flutterlib/common/constants/constants.dart';
import 'package:flutterlib/common/utils/utils.dart';
import 'package:thrio/thrio.dart';
import '../channel/asynchronous_method_channel.dart';

class ChannelDataCenter {
  ChannelDataCenter._();

  static final _instance = new ChannelDataCenter._();

  factory ChannelDataCenter() {
    return _instance;
  }


  static var _asyncChannel = AsynchronousMethodChannel('async_channel');
  static const _methodChannel = const MethodChannel('method_channel');
  Map<String, Object> memMap = new Map();

  void init() {
    trueDebugPrint("flutterchannel channeldatacenter init");
    _methodChannel.setMethodCallHandler((call) async {
      return _handle(call);
    });
    //在这里直接invoke不知道为什么不行，找不到native端的handler
  }

  Future<T> invokeSync<T>(String methodName, [arguments]) async {
    try {
      T data =
          await _asyncChannel.invokeAsynchronousMethod(methodName, [arguments]);
      return data;
    } catch (e) {
      print("flutterchannel invokeSync" + e.toString());
      // throw PlatformException(code: 'getdata from native error');
    }
  }

  Future<T> invoke<T>(String methodName, [arguments]) async {
    try {
      T data = await _methodChannel.invokeMethod(methodName, [arguments]);
      return data;
    } catch (e, stack) {
      print("flutterchannel invoke" + e.toString());
      print("flutterchannel invoke" + stack.toString());
      // throw PlatformException(code: 'getdata from native error');
    }
  }

  Future<T> invokeMethod<T>(String methodName, arguments) async {
    try {
      T data = await _methodChannel.invokeMethod(methodName, arguments);
      return data;
    } catch (e, stack) {
      print("flutterchannel invoke" + e.toString());
      print("flutterchannel invoke" + stack.toString());
      // throw PlatformException(code: 'getdata from native error');
    }
  }

  _handle(MethodCall call) {
    // var key = call.method + call.arguments.toString();
    // if(memMap.containsKey(key)){
    //   return memMap[key];
    // }
    trueDebugPrint("flutterchannel" + "_handle" + call.method);

    switch (call.method) {
      case "login":
        trueDebugPrint("flutterchannel" + "login" + call.arguments);
        var str = call.arguments.toString();
        loginIn(str);
        break;
      case "loginout":
        trueDebugPrint("flutterchannel" + "loginout" + call.arguments);
        loginOut();
        break;
      case "ready":
        trueDebugPrint("flutterchannel" +  "ready" + call.arguments);
        invoke<String>("ready","");
        break;
      case "appversion":
        trueDebugPrint("flutterchannel" + "appversion" + call.arguments);
        setAppVersion(call.arguments.toString());
        break;
      case "interests":
        trueDebugPrint("flutterchannel" +  "get interests");
        setInterests(call.arguments.toString());
        break;
      case "unReadUU":
        final SettingsStore store =
        Provider.of<SettingsStore>(currentContext, listen: false);
        store.setUnReadUUMsgCount(call.arguments);
        break;
      case "unReadUCG":
        final SettingsStore store =
        Provider.of<SettingsStore>(currentContext, listen: false);
        store.setUnReadUCGMsgCount(call.arguments);
        break;
      case "unReadTodo":
        final SettingsStore store =
        Provider.of<SettingsStore>(currentContext, listen: false);
        store.setUnReadTodoMsgCount(call.arguments);
        break;

      case "SetSP":
        SpUtils().init().then((sp) {
          trueDebugPrint("flutterchannel"+ "sp put");

          final key = call.arguments["key"];
          final value = call.arguments["value"];
          if (key != null && value != null) {
            SpUtils.putString(key, value);
          }
        });
        return 'success';
        break;
    }

    throw PlatformException(code: 'error code');
  }
}
